package com.woniuxy.dr_mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dr_mall.entity.DrProducts;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
public interface DrProductsMapper extends BaseMapper<DrProducts> {


    /**
     * 根据主键查询商品，在商品详情页展示
     *
     * @param id 主键
     * @return 实体类
     * @author yy
     */
    @Select("select * from dr_products where id=#{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "picture", one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.getPictureByProductId"
            ))
    })
    DrProducts getProductById(int id);


    /**
     * @return java.util.List<com.woniuxy.dr_mall.entity.DrProducts>
     * @description:得到所有商品列表
     * @Author 张哲轩
     * @Date 2021/8/16 11:45
     */
    @Select("select * from dr_products")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "pictures", many = @Many(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.getPicturesByProductId"
            ))
    })
    List<DrProducts> getAllProducts();

    /**
     * 查询人气最高的产品
     * @param popular   人气
     * @return  人气最高list
     */
    @Select("select * from dr_products where status='y' and popular=#{popular}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "id",property = "picture",one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.getPictureByProductId"
            ))
    })
    List<DrProducts> getByPopular(String popular);

    @Select("select * from dr_products where status='y' and favor=#{favor}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "id",property = "picture",one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.getPictureByProductId"
            ))
    })
    List<DrProducts> getByFavor(String favor);
    @Select("select * from dr_products where status='y' and hot=#{hot}")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "id",property = "picture",one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrPictureMapper.getPictureByProductId"
            ))
    })
    List<DrProducts> getByHot(String hot);

    @Select("select min(dsp.price) from dr_spec_price dsp,dr_products dp where dsp.productid=dp.id and dp.id=#{productId}")
    BigDecimal getMinPrice(Integer productId);


    //通过商品id查询商品的名字和商品的图片 叶官鑫 2021-8-18
    @Select("select name from dr_products where id=#{productId}")
    String selectProductByid(int productId);
}
